package com.tensquare.controller;

import entity.PageResult;
import entity.Result;

import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.tensquare.pojo.Label;
import com.tensquare.service.LabelService;

import java.util.List;

@RestController
@RequestMapping("label")
@CrossOrigin()
public class LabelController {

    @Autowired
    private LabelService labelService;
    //查询所有的标签列表
    @GetMapping
    public Result findAll(){
       List<Label> labelList=labelService.findAll();
       return new Result(true,StatusCode.OK,"查询全部标签列表",labelList);
    }
    //根据id查询标签
    @GetMapping(value = "{labelId}")
    public Result findByid(@PathVariable String labelId){
       Label labelList=labelService.findByid(labelId);
        return new Result(true,StatusCode.OK,"查找成功",labelList);
    }

    //新增标签
    @PostMapping()
    public Result addLabel(@RequestBody Label label){
        labelService.addLabel(label);
        return new Result(true,StatusCode.OK,"新增标签");
    }
    //根据id修改标签
    @PutMapping(value = "{labelId}")
    public Result updateLabel(@RequestBody Label label,@PathVariable String labelId){
        //通过前端页面传来的id加入到label中,形成完成的label
        label.setId(labelId);
        labelService.updateLabel(label);
        return new Result(true,StatusCode.OK,"修改成功");
    }
    //根据id删除标签
    @DeleteMapping(value = "{labelId}")
    public Result deleteLabel(@PathVariable String labelId){
        labelService.deleteLabel(labelId);
        return new Result(true,StatusCode.OK,"删除成功");
    }
    //根据条件查询
    @PostMapping(value = "/search")
    public Result searchLabel(@RequestBody Label label){
       List<Label> labelList=labelService.searchLabel(label);
       return new Result(true,StatusCode.OK,"条件查询全部标签列表",labelList);
    }
    //根据标签条件查询+分页
    @PostMapping(value = "search/{page}/{size}")
    public Result searchPageLabel(@RequestBody Label label,@PathVariable int page,@PathVariable int size){
        PageResult<Label> pageResult = labelService.searchPageLabel(label, page, size);
        return new Result(true,StatusCode.OK,"条件查询带分页面",pageResult);
    }
}
